package com.jisf.driver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jisf.driver.entity.enumtype.EnumContext;
import com.jisf.driver.entity.enumtype.StatusEnum;
import com.jisf.driver.entity.enumtype.VisibleEnum;
import com.jisf.driver.entity.vo.MenuListVo;
import com.jisf.driver.entity.po.Menu;
import com.jisf.driver.exception.SystemException;
import com.jisf.driver.filter.JwtAuthenticationTokenFilter;
import com.jisf.driver.mapper.MenuMapper;
import com.jisf.driver.response.IErrorCode;
import com.jisf.driver.response.ResultCode;
import com.jisf.driver.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jisf.driver.utils.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 权限表 服务实现类
 * </p>
 *
 * @author jisf
 * @since 2022-04-08
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 获取侧边栏菜单列表
     * @return
     */
    @Override
    public List<MenuListVo> getMenuListAside() {
        try {
            List<MenuListVo> selectMenuLists = menuMapper.selectMenuListByUserId(UserUtil.userIdThreadLocal.get());
            List<MenuListVo> menuListVos = organizeMenu(selectMenuLists, 2);
            menuListVos.sort(new Comparator<MenuListVo>() {
                @Override
                public int compare(MenuListVo o1, MenuListVo o2) {
                    return o1.getId().compareTo(o2.getId());
                }
            });
            return menuListVos;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 获取权限列表
     * @return
     */
    @Override
    public List<MenuListVo> menuList() {
        try {
            List<MenuListVo> menuListVos = menuMapper.selectMenusList();
            return organizeMenu(menuListVos);
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 整理菜单（所有菜单）
     * @param menuListVos
     * @return
     */
    public List<MenuListVo> organizeMenu(List<MenuListVo> menuListVos){
        return organizeMenu(menuListVos, 10);
    }

    /**
     * 整理菜单
     * @param menuListVos
     * @param count 菜单级数
     * @return
     */
    public List<MenuListVo> organizeMenu(List<MenuListVo> menuListVos, int count){
        List<MenuListVo> menuLists = new ArrayList<>();
        for (MenuListVo menuListVo : menuListVos) {
            if (menuListVo.getPid() == 0) {
                menuLists.add(menuListVo);
                getMenuListChildren(menuListVo, menuListVos, 1, count);
            }
        }
        return menuLists;
    }

    /**
     * 获取侧边栏多级菜单（回溯）
     * @param menuList
     * @param selectMenuLists
     * @param n 当前的menuList菜单级数
     * @param count 总共需要获取的菜单数
     * @return
     */
    private MenuListVo getMenuListChildren(MenuListVo menuList, List<MenuListVo> selectMenuLists, int n, int count) {
        n += 1;
        if (menuList.getChildren() == null) {
            menuList.setChildren(new LinkedList<>());
        }
        for (MenuListVo selectMenuList : selectMenuLists) {
            if (menuList.getId().equals(selectMenuList.getPid())) {
                menuList.getChildren().add(selectMenuList);
                if (n < count){
                    getMenuListChildren(selectMenuList, selectMenuLists, n, count);
                }
            }
        }
        return menuList;
    }

    /**
     * 获取所有权限
     * @return
     */
    @Override
    public List<MenuListVo> getAllMenus() {
        try {
            List<MenuListVo> menuListVos = menuMapper.selectAllMenus();
            List<MenuListVo> allMenus = organizeMenu(menuListVos);
            return allMenus;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 改变权限状态
     * @param menuId
     * @param status
     * @return
     */
    @Override
    public IErrorCode menuChangeStatus(Integer menuId, String status) {
        try {
            UpdateWrapper<Menu> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("status", StatusEnum.getEnum(status));
            updateWrapper.eq("id", menuId);
            int update = menuMapper.update(new Menu(), updateWrapper);
            return update == 0 ? ResultCode.MENU_STATUS_UPDATE_FAILED : ResultCode.MENU_STATUS_UPDATE_SUCCESS;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }

    /**
     * 改变权限是否显示在菜单栏
     * @param menuId
     * @param visible
     * @return
     */
    @Override
    public IErrorCode menuChangeVisible(Integer menuId, String visible) {
        try {
            UpdateWrapper<Menu> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("visible", VisibleEnum.getEnum(visible));
            updateWrapper.eq("id", menuId);
            int update = menuMapper.update(new Menu(), updateWrapper);
            return update == 0 ? ResultCode.MENU_VISIBLE_UPDATE_FAILED : ResultCode.MENU_VISIBLE_UPDATE_SUCCESS;
        }catch (Exception e){
            throw new SystemException(e);
        }
    }
}
